//
//  VideoViewController.m
//  WangYiNews
//
//  Created by lanou3g on 15/10/13.
//  Copyright (c) 2015年 zhengming. All rights reserved.
//

#import "VideoViewController.h"
#import "VideoTableViewCell.h"
#import "FinderTableViewCell.h"
#import "VideoCollectionViewCell.h"

@interface VideoViewController () <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UIView *barView; // 容器
@property (nonatomic, strong) UILabel *lineLabel; // 下滑白线
@property (nonatomic, strong) UIButton *TVBtn; // 视频
@property (nonatomic, strong) UIButton *VideoBtn; // 电台

@end

@implementation VideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO; // 设置内距
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self subView];
    // 加载数据
    [[Manager share] setupTVDataWithBlock:^{
        [self.TVTableView reloadData];
    }];
    [[Manager share] setupVideoDataWithBlock:^{
        [self.VideoCollectionView reloadData];
    }];
}

- (void)subView
{
    self.TVBar = [[UINavigationBar alloc] init];
    self.TVBar.frame = CGRectMake(0, 0, kScreenWidth, 50);
    self.TVBar.barTintColor = [UIColor redColor];
    [self.view addSubview:self.TVBar];
    
    self.barView = [[UIView alloc] initWithFrame:CGRectMake(15, 20, 100, 30)];
    self.barView.backgroundColor = [UIColor clearColor];
    [self.TVBar addSubview:self.barView];
    
    self.TVBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.TVBtn.frame = CGRectMake(0, 0, 50, 30);
    self.TVBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.TVBtn.alpha = 1;
    [self.TVBtn setTitle:@"视频" forState:UIControlStateNormal];
    [self.TVBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [self.TVBtn addTarget:self action:@selector(TVBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.barView addSubview:self.TVBtn];
    
    self.VideoBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.VideoBtn.frame = CGRectMake(50, 0, 50, 30);
    [self.VideoBtn setTitle:@"电台" forState:UIControlStateNormal];
    [self.VideoBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.VideoBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.VideoBtn.alpha = 0.6;
    [self.VideoBtn addTarget:self action:@selector(VideoBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.barView addSubview:self.VideoBtn];
    
    self.lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 28, 50, 2)];
    self.lineLabel.backgroundColor = [UIColor whiteColor];
    [self.barView addSubview:self.lineLabel];
    
    self.ScrollView.delegate = self;
    
    self.TVTableView.delegate = self;
    self.TVTableView.dataSource = self;
    self.TVTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.VideoCollectionView.dataSource = self;
    self.VideoCollectionView.delegate = self;
    
    // 注册cell
    [self.TVTableView registerNib:[UINib nibWithNibName:@"VideoTableViewCell" bundle:nil] forCellReuseIdentifier:@"VideoCell"];
    
    // 注册collectionviewCell
    [self.VideoCollectionView registerNib:[UINib nibWithNibName:@"VideoCollectionViewCell"  bundle:nil]forCellWithReuseIdentifier:@"VideoCell"];
//    [self.VideoCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
}

#pragma mark -实现代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [Manager share].TVArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
         VideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VideoCell" forIndexPath:indexPath];
        TVModel *model = [Manager share].TVArray[indexPath.row];
        [cell.videoImageView sd_setImageWithURL:[NSURL URLWithString:model.cover]];
        [cell.titleBtn setTitle:model.title forState:(UIControlStateNormal)];
        [cell.detailBtn setTitle:model.TVDescription forState:(UIControlStateNormal)];
        cell.timeLabel.text = [NSString stringWithFormat:@"%.2d:%.2d", [model.length intValue] / 60, [model.length intValue] % 60];
        cell.numberLabel.text = [NSString stringWithFormat:@"%@", model.playCount];
        cell.numberOfCommentLabel.text = [NSString stringWithFormat:@"%@", model.playCount];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 300;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 60)];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60;
}

#pragma mark -实现UICollectionViewDataSource, UICollectionViewDelegate代理方法
// 设置每个分区中item的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}

// 设置cell的样式
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    VideoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"VideoCell" forIndexPath:indexPath];
    [cell.layer setBorderWidth:0.5f];
//    [cell.layer setBorderColor:[UIColor grayColor].CGColor];
    [cell.layer setShadowOffset:CGSizeMake(-1, -5)];
    [cell.layer setShadowColor:[UIColor grayColor].CGColor];
    
    VideoModel *model = [Manager share].videoArray[indexPath.section][indexPath.row];
    
    [cell.videoImageView sd_setImageWithURL:[NSURL URLWithString:model.imgsrc]];
    cell.titleLabel.text = model.tname;
    cell.detailLabel.text = model.title;
    return cell;
}

// 设置分区个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    NSLog(@"Video = %lu", [Manager share].videoArray.count);
    return [Manager share].videoArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(110, 160);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath];
    header.frame = CGRectMake(0, 0, kScreenWidth, 50);
    NSLog(@"---%@", header);
    return header;
}



#pragma -mark -滑动scrollView页面触发的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    NSLog(@"%f", scrollView.contentOffset.x);
    CGRect frame = self.lineLabel.frame ;
    frame.origin.x = 50 * scrollView.contentOffset.x / kScreenWidth;
    self.lineLabel.frame = frame;
    self.TVBtn.alpha = 1 - 0.4 * scrollView.contentOffset.x / kScreenWidth;
    self.VideoBtn.alpha = 0.6 + 0.4 * scrollView.contentOffset.x / kScreenWidth;
}

#pragma mark -按钮触发事件
- (void)TVBtnAction:(UIButton *)TVBtn
{
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = self.lineLabel.frame;
        frame.origin.x = 0;
        self.lineLabel.frame = frame;
        self.ScrollView.contentOffset = CGPointMake(0, 0);
        self.TVBtn.alpha = 1;
        self.VideoBtn.alpha = 0.6;
    }];
    
}

- (void)VideoBtnAction:(UIButton *)VideoBtn
{
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = self.lineLabel.frame;
        frame.origin.x = self.barView.frame.size.width / 2;
        self.lineLabel.frame = frame;
        self.ScrollView.contentOffset = CGPointMake(kScreenWidth, 0);
        self.TVBtn.alpha = 0.6;
        self.VideoBtn.alpha = 1;
    }];
}

#pragma mark -自定义按钮
//- (UIButton *)creatBtnWithImageView:(UIImageView *)imageView imageFrame:(CGRect *)imageFrame label:(UILabel *)label labelFrame:(CGRect)labelFrame
//{
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
